/**
 * 
 */
package com.wolfpire.workflow.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wolfpire.system.common.springmvc.PrincipalArgument;
import com.wolfpire.system.model.Org;
import com.wolfpire.system.model.Role;
import com.wolfpire.system.model.User;
import com.wolfpire.system.service.OrgService;
import com.wolfpire.system.service.RoleService;
import com.wolfpire.workflow.em.ActorType;
import com.wolfpire.workflow.om.NodeActor;
import com.wolfpire.workflow.platform.PlatformService;
import com.wolfpire.workflow.spring.NodeActorService;

/**
 * 节点控制器
 * @author lihd
 *
 */

@Controller
@RequestMapping("/node")
public class NodeController {
	
	@Resource private NodeActorService nodeActorService;
	
	@Resource private PlatformService platformService;
	
	@Resource private OrgService orgService;
	
	@Resource private RoleService roleService;
	
	@ResponseBody
	@RequestMapping(value = "/getNodeActorUsers", method = {RequestMethod.GET, RequestMethod.POST})
	public Map<String, Object> getNodeActorUsers(
				@RequestParam(value = "nodeId", required = false) Long nodeId,
				@RequestParam(value = "instanceId", required = false) Long instanceId
			) {
		
		Map<String, Object> data = new HashMap<String, Object>();
		try {
			if(nodeId != null) {
				String userIds = "";
				String userNames = "";
				
				List<User> users = new ArrayList<User>();
//				List<Map<String, Object>> actors = nodeActorService.queryNodeActorListByNodeId(nodeId);
				List<NodeActor> actors = this.nodeActorService.getNodeActorsByNodeId(nodeId);
				for (NodeActor na : actors) {
					if(ActorType.USER.getIndex().equals(na.getActorType())){
						userIds += na.getActorId().toString() + ",";
						userNames += na.getActorName() + ",";
					}else if(ActorType.ROLE.getIndex().equals(na.getActorType())){
						Long roleId = na.getActorId();
						users = this.platformService.getUsersByRoleId(roleId);
						for (User u : users) {
							userIds += u.getId().toString() + ",";
							userNames += u.getNickName().toString() + ",";
						}
					}else if(ActorType.ORG.getIndex().equals(na.getActorType())){
						Long orgId = na.getActorId();
						users = this.platformService.getUsersByOrgId(orgId);
						for (User u : users) {
							userIds += u.getId().toString() + ",";
							userNames += u.getNickName().toString() + ",";
						}
					}
				}
				if(!"".equals(userIds)){
					userIds = userIds.substring(0,userIds.length()-1);
				}
				if(!"".equals(userNames)){
					userNames = userNames.substring(0,userNames.length()-1);
				}
				data.put("result", "success");
				data.put("userIds", userIds);
				data.put("userNames", userNames);
			}
		} catch (Exception e) {
			data.put("result", "error");
			e.printStackTrace();
		}
		return data;
	}

	@ResponseBody
	@RequestMapping("/getNodeActors")
	public List<Map<String, Object>> getNodeActors(@PrincipalArgument User user,
			@RequestParam(value = "nodeId", required = false) Long nodeId,
			@RequestParam(value = "nodeType", required = false) String nodeType
			) {

		List<Map<String, Object>> resultMap = new ArrayList<Map<String,Object>>();
		if("org".equals(nodeType)){
			List<Org> orgs = orgService.list(null);
			for(Org org : orgs){
				Map<String, Object> map = new HashMap<String, Object>();
				if(org.getParentId() == null){
					map.put("ACTOR_TYPE", "1");
					map.put("ACTOR_ID", org.getId());
					map.put("ACTOR_NAME", org.getName());
					resultMap.add(map);
				}
			}
		} else if ("selectByRole".equals(nodeType)) {
			List<Role> roles = this.roleService.list(null);
			if (CollectionUtils.isNotEmpty(roles)) {
				for (Role r : roles) {
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("ACTOR_TYPE", ActorType.ROLE.getIndex().toString());
					map.put("ACTOR_ID", r.getId());
					map.put("ACTOR_NAME", r.getName());
					resultMap.add(map);
				}
			}
		} else{
			if(nodeId != null) {
				List<NodeActor> actors = this.nodeActorService.getNodeActorsByNodeId(nodeId);
				for (NodeActor na : actors) {
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("ACTOR_TYPE", na.getActorType());
					map.put("ACTOR_ID", na.getActorId());
					map.put("ACTOR_NAME", na.getActorName());
					resultMap.add(map);
				}
			}
		}
		return resultMap;
	
	}
	
}
